package org.lease.web.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.lease.common.result.Result;
import org.lease.model.dto.FeeDTO;
import org.lease.model.entity.FeeKey;
import org.lease.model.entity.FeeValue;
import org.lease.service.FeeKeyService;
import org.lease.service.FeeValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "杂费信息管理")
@RestController
@RequestMapping("/admin/fee")
public class FeeController {
    @Autowired
    private FeeKeyService feeKeyService;
    @Autowired
    private FeeValueService feeValueService;

    @Operation(summary = "查询所有杂费信息")
    @GetMapping("/list")
    public Result<List<FeeDTO>> getAll() {
        List<FeeDTO> feeDTOS = feeKeyService.listWithValue();
        log.info("查询所有杂费信息，结果：{}", feeDTOS);
        return Result.success(feeDTOS);
    }

    @Operation(summary = "保存或更新杂费属性名")
    @PostMapping("/key/saveOrUpdate")
    public Result saveOrUpdateKey(@RequestBody FeeKey feeKey) {
        feeKeyService.saveOrUpdate(feeKey);
        return Result.success();
    }

    @Operation(summary = "根据 ID 删除属性名")
    @DeleteMapping("/key/deleteById")
    public Result removeKeyWithValue(Long feeKeyId) {
        feeKeyService.removeById(feeKeyId);
        feeValueService.removeByKeyId(feeKeyId);
        return Result.success();
    }

    @Operation(summary = "保存或更新杂费属性值")
    @PostMapping("/value/saveOrUpdate")
    public Result saveOrUpdateValue(@RequestBody FeeValue feeValue) {
        feeValueService.saveOrUpdate(feeValue);
        return Result.success();
    }

    @Operation(summary = "根据 ID 删除属性值")
    @DeleteMapping("/value/deleteById")
    public Result removeValue(Long id) {
        feeValueService.removeById(id);
        return Result.success();
    }
}
